import {
    message
} from "ant-design-vue";
import router from "@/router";
import request from "@/util/httpUtils";


export default {
    get(url, params) {
        const urlPath = this.api[url];
        const token = localStorage.getItem("token") || null;
        const header = token ?
            {
                Authorization: "Bearer " + token,
                //   token,
            } :
            null;
        return new Promise((resolve, reject) => {
            request({
                url: urlPath,
                method: "get",
                headers: header,
                params,
            }).then(res => {
                if (res.data.status == "0") {
                    resolve(res.data);
                } else {
                    message.warning(res.data.message);
                    reject(res.data);
                }
            }).catch((error) => {
                if(error.response.status === 403){
                    this.gotoLogin();
                    message.warning({content:"登录超时，请重新登录",key:'timeout'});
                }else if(error.response.status === 500){
                    localStorage.removeItem('token');
                    this.gotoLogin();
                    message.warning({content:"登录超时，请重新登录",key:'timeout'});
                }else{
                    message.warning({content:"请求错误",key:'error'});
                }
            });
        })
    },
    post(url, data) {
        const urlPath = this.api[url];
        const token = localStorage.getItem("token") || null;
        const header = {
            Authorization: "Bearer " + token,
        };
        return new Promise((resolve, reject) => {
            request({
                url: urlPath,
                method: "post",
                headers: header,
                data,
            }).then(res => {
                if (res.data.status == "0") {
                    resolve(res.data,res.headers);
                } else {
                    if(res.data.status != '1022'){
                        message.warning(res.data.message);
                    }
                    reject(res.data);
                }
            }).catch( (error)  => {
                if(error.response.status === 403){
                    this.gotoLogin();
                    message.warning({content:"登录超时，请重新登录",key:'timeout'});
                }else if(error.response.status === 500){
                    localStorage.removeItem('token');
                    this.gotoLogin();
                    message.warning({content:"登录超时，请重新登录",key:'timeout'});
                }else{
                    message.warning({content:"请求错误",key:'error'});
                }
            });
        })
    },
	downloadBlob(url, data){
		const urlPath = this.api[url];
        const token = localStorage.getItem("token") || null;
        const header = token ? {
            Authorization: "Bearer " + token,
        } : null;
        return new Promise((resolve) => {
            request({
                url: urlPath,
                method: "post",
                headers: header,
                data,
				responseType:"blob",
            }).then(res => {
				resolve(res);
            }).catch( (error)  => {
                if(error.response.status === 403){
                    this.gotoLogin();
                    message.warning({content:"登录超时，请重新登录",key:'timeout'});
                }else if(error.response.status === 500){
                    localStorage.removeItem('token');
                    this.gotoLogin();
                    message.warning({content:"登录超时，请重新登录",key:'timeout'});
                }else{
                    message.warning({content:"请求错误",key:'error'});
                }
            });
        })
	},
    uploadFileByFormData(data) {
        const token = localStorage.getItem("token") || null;
        const header = {
            //   Authorization: "Bearer " + token,
            token,
            "Content-Type": "multipart/form-data",
        };
        const formdata = new FormData();
        formdata.append("file", data);
        return request({
            url: this.api["上传资源"],
            method: "post",
            headers: header,
            data: formdata,
        });
    },
    api: {
        '生成图形验证码唯一id': "/generateImageVerifyCodeId.do",
        '导入单位': "/importFirm.do",
        '导出单位': "/exportFirm.do",
        '生成图形验证码': "/generateImageVerifyCode.do",
        '登录': "/sign.do",
        '发送验证码': "/sendSmsCode.do",
        '修改密码': "/resetLoginPwWithVCode.do",
        '获取当前管理员权限': "/init.do",
        '登出': "/signout.do",
        'getFirmDetail': "/getFirmDetail.do",
        '企业列表': "/getFirmList.do",
        '创建企业': "/createFirm.do",
        '编辑企业': "/editFirm.do",
        '企业详情': "/getFirmDetail.do",
        '企业详情-端口资产列表': "/getAssetPortListFromFirmDetail.do",
        '企业详情-网站测绘列表': "/getAssetAppListFromFirmDetail.do",
        '企业详情-漏洞列表': "/getVulnListFromFirmDetail.do",
        '企业性质集合(用于选择框)': "/getFirmNatureTypeOptions.do",
        '首页': "/main.do",
        // 日志
        '管理员操作日志列表': "/getNoLogList.do",
        '管理员操作日志详情': "/getNoLogDetail.do",
        //管理员
        '管理员列表': "/getNoUserList.do",
        '岗位集合': "/getNoPostOptions.do",
        '创建管理员': "/createNoUser.do",
        '编辑管理员': "/editNoUser.do",
        '删除管理员': "/deleteNoUser.do",
        //角色
        '角色列表': "/getNoRoleList.do",
        '角色集合': "/getNoRoleOptions.do",
        '创建角色': "/createNoRole.do",
        '编辑角色': "/editNoRole.do",
        '删除角色': "/deleteNoRole.do",
        '角色集合 (用于选择框)': "/getNoRoleOptions.do",
        // 菜單
        '创建菜单': "/createNoPagePath.do",
        '删除菜单': "/deleteNoPagePath.do",
        '编辑菜单': "/editNoPagePath.do",
        '菜单管理': "/getNoPagePathList.do",
        //消息
        '消息列表':'/getMessageList.do',
        //通报
        '通报列表':'/getReportList.do',
        '创建通报':'/createReport.do',
        '处理通报':'/disposeReport.do',
        //检验中心
        '检验列表(检验中心)':'/getInspectionList.do',
        '检验列表(工作台)':'/getInspectionListOfMain.do',
        '检验列表(单位详情)':'/getInspectionListOfFirmDetail.do',
        '检验详情':'/getInspectionDetail.do',
        '检验详情-风险列表':'/getRiskListOfInspectionDetail.do',
        //漏洞
        '处理漏洞':'/disposeReport.do',
        '漏洞详情':'/getVulnDetail.do',
        '漏洞列表':'/getVulnList.do',
        
    },
    getCookie(name) {
        for (var s of document.cookie.split(";")) {
            let arr = s.split("=");
            if (arr[0].trim() == name) {
                return arr[1];
            }
        }
        return "";
    },
    gotoLogin(){
        router.push("/login");
    }
};